<template>
  <div>
    <!-- 顶部单选框 -->
    <div>
      未接收<input
        type="radio"
        name="aa"
        @click="showDistributionState"
        checked="checked"
      />
      已接收<input type="radio" name="aa" @click="showAll" />
    </div>
    <!-- 带表单的弹出框 -->
    <div>
      <el-dialog title="药品不合格信息" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="药品名字" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="不合格数量" :label-width="formLabelWidth">
            <el-input
              v-model="form.distributionNumber"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="不合格原因" :label-width="formLabelWidth">
            <el-input v-model="form.description" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="error">确 定</el-button>
        </div>
      </el-dialog>
    </div>

    <!-- 主要展示的表格 -->
    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search ||
            data.distributionName.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
    >
      <el-table-column label="配送时间" prop="dateManufacture">
      </el-table-column>
      <el-table-column label="配送人" prop="distributionName">
      </el-table-column>
      <el-table-column label="配送数量" prop="distributionNumber">
      </el-table-column>
      <el-table-column label="是否接收" prop="distributionState">
      </el-table-column>
      <el-table-column label="药品名字" prop="medicine.name"> </el-table-column>
      <el-table-column label="生产日期" prop="dateManufacture">
      </el-table-column>

      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button
            v-if="look"
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            >接收</el-button
          >
          <el-button
            v-if="look"
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >药品有误</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <br /><br />
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[8, 10, 12]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  created() {
    this.$axios
      .get(
        "http://localhost:8080/medicines/showAllDistribution/" +
          this.currentPage +
          "/" +
          this.page
      )
      .then((src) => {
        console.log(src);
        this.tableData = src.data.data.list;
        this.pageInfo = src.data.data;
      });
  },
  data() {
    return {
      tableData: [],
      search: "",
      look: true,
      dialogFormVisible: false,
      form: {
        name: "",
        distributionNumber: "",
        description: "",
        row: {},
      },
      formLabelWidth: "100px",
      currentPage: 1,
      page: 8,
      pageInfo: {},
    };
  },
  methods: {
    handleSizeChange(val) {
      this.page = val;
      if (this.look) {
        this.$axios
          .get(
            "http://localhost:8080/medicines/showAllDistribution/" +
              this.currentPage +
              "/" +
              this.page
          )
          .then((src) => {
            console.log(src);
            this.tableData = src.data.data.list;
            this.pageInfo = src.data.data;
          });
      } else {
        this.$axios
          .get(
            "http://localhost:8080/medicines/showAllDistributionYes/" +
              this.currentPage +
              "/" +
              this.page
          )
          .then((src) => {
            this.tableData = src.data.data.list;
            this.pageInfo = src.data.data;
          });
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      if (this.look) {
        this.$axios
          .get(
            "http://localhost:8080/medicines/showAllDistribution/" +
              this.currentPage +
              "/" +
              this.page
          )
          .then((src) => {
            console.log(src);
            this.tableData = src.data.data.list;
            this.pageInfo = src.data.data;
          });
      } else {
        this.$axios
          .get(
            "http://localhost:8080/medicines/showAllDistributionYes/" +
              this.currentPage +
              "/" +
              this.page
          )
          .then((src) => {
            this.tableData = src.data.data.list;
            this.pageInfo = src.data.data;
          });
      }
    },
    error() {
      this.dialogFormVisible = false;
      this.in();
    },
    //调用药品有问题的接口把有问题的信息插入问题表
    in() {
      this.$axios
        .post("http://localhost:8080/medicines/issue", {
          medicineId: this.form.row.medicine.id,
          description: this.form.description,
          cont: this.form.distributionNumber,
        })
        .then((src) => {
          //再次发送axios请求将剩余没问题的药品数量存入待入库表
          this.$axios
            .post(
              "http://localhost:8080/medicines/acceptance/" +
                JSON.parse(localStorage.getItem("user")).username,
              {
                medicineId: this.form.row.medicine.id,
                inNum:
                  this.form.row.distributionNumber -
                  this.form.distributionNumber,
                dateManufacture: this.form.row.dateManufacture,
              }
            )
            .then((src) => {
              if (src.data.code == 200) {
                this.$message({ message: src.data.msg, type: "success" });
              } else {
                this.$message.error(src.data.msg);
              }
              this.showDistributionState();
              this.$router.push("/ShowStorageIn");
            });
        });
    },
    showAll() {
      this.look = false;
      this.$axios
        .get(
          "http://localhost:8080/medicines/showAllDistributionYes/" +
            this.currentPage +
            "/" +
            this.page
        )
        .then((src) => {
          this.tableData = src.data.data.list;
          this.pageInfo = src.data.data;
        });
    },
    showDistributionState() {
      this.look = true;
      this.$axios
        .get(
          "http://localhost:8080/medicines/showAllDistribution/" +
            this.currentPage +
            "/" +
            this.page
        )
        .then((src) => {
          console.log(src);
          this.tableData = src.data.data.list;
          this.pageInfo = src.data.data;
        });
    },
    handleEdit(index, row) {
      // 点击接收数据存储到入库表中

      this.$axios
        .post(
          "http://localhost:8080/medicines/acceptance/" +
            JSON.parse(localStorage.getItem("user")).username,
          {
            medicineId: row.medicine.id,
            inNum: row.distributionNumber,
            dateManufacture: row.dateManufacture,
          }
        )
        .then((src) => {
          if (src.data.code == 200) {
                this.$message({ message: src.data.msg, type: "success" });
              } else {
                this.$message.error(src.data.msg);
              }
          this.showDistributionState();
          this.$router.push("/ShowStorageIn");
        });
    },
    //药品有误的方法
    handleDelete(index, row) {
      this.dialogFormVisible = true;
      this.form.name = row.medicine.name;
      this.form.row = row;
    },
  },
};
</script>
<style lang="less" scoped></style>
